﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using Spotless.Data;

namespace SpotlessWeb.Models
{
    public class DrillDownItemViewModel
    {
        public DrillDownItemViewModel(string name, string property, DrillDown item, IEnumerable<DrillDown> options, bool allowEdit)
        {
            this.Name = name;
            this.Property = property;
            this.Item = item;
            this.Options = options;
            this.AllowEdit = allowEdit;
        }

        public bool AllowEdit { get; private set; }
        public string Name { get; private set; }
        public string Property { get; private set; }
        public DrillDown Item { get; private set; }
        public IEnumerable<DrillDown> Options { get; private set; }
        private IEnumerable<DrillDown> _SelectedItems;
        public IEnumerable<DrillDown> SelectedItems
        {
            get
            {
                if (_SelectedItems == null)
                {
                    var tmp = new List<DrillDown>();
                    var current = this.Item;
                    while (current != null)
                    {
                        tmp.Add(current);
                        current = current.Parent != null && current.Parent.Type == current.Type ? current.Parent : (DrillDown)null;
                    }
                    tmp.Reverse();
                    _SelectedItems = tmp.AsEnumerable();
                }
                return _SelectedItems;
            }
        }
    }
}
